
package editor.map.elements
{
	import com.bit101.components.Label;
	
	import editor.common.Common;
	import editor.util.NumberTool;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.ui.Keyboard;
	
	/**
	 * @author 缺硒
	 */
	public class Npc extends Sprite
	{
		public static var NpcBmd:BitmapData;
		
		private var _tileShow:Tile;
		
		protected var _mainContainer:Sprite;
		private var _lp:Point;
		private var _npcId:String = "1";
		private var _isSelected:Boolean = false;
		private var _npcIdLabel:TextField;
		
		protected static var __filter:GlowFilter = new GlowFilter(0x00ffff, 1, 20, 20, 10);
		
		public var npcBmp:Bitmap;
		
		// 构造函数
		public function Npc()
		{
			//			if(NpcBmd == null)
			//			{
			//				NpcBmd = new Bg().bitmapData;
			//				NpcBmd.colorTransform(NpcBmd.rect, new ColorTransform(1,1,0));
			//			}
			//			npcBmp = new Bitmap(NpcBmd);
			//			npcBmp.x = - npcBmp.width / 2;
			//			npcBmp.y = - 90;
			//			npcBmp.alpha = 0.98;
			
			_mainContainer = new Sprite();
			
//			_tileShow = new Tile();
//			_tileShow.draw(0x00ff00);
//			_tileShow.x = -Tile.TILE_WIDTH / 2;
//			_tileShow.y = -Tile.TILE_WIDTH / 2
			
			//			this.addChild(npcBmp);
//			_mainContainer.addChild(_tileShow);
			
			_npcIdLabel = new TextField();
			_npcIdLabel.autoSize = TextFieldAutoSize.LEFT;
			_npcIdLabel.x = - 20;
			_npcIdLabel.y = - 100;
			_npcIdLabel.background = true;
			_npcIdLabel.backgroundColor = 0xffffff;
			
			_mainContainer.addChild(_npcIdLabel);
			addChild(_mainContainer);
			_mainContainer.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			_mainContainer.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		
		public function get isSelected():Boolean
		{
			return _isSelected;
		}
		
		public function set isSelected(value:Boolean):void
		{
			_isSelected = value;
			
			if(_isSelected)
			{
				if(this is Teleport)
					this.filters = [__filter];
				else
					_npcIdLabel.filters = [__filter];
				stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyUp);
			}
			else
			{
				if(this is Teleport)
					this.filters = null;
				else
					_npcIdLabel.filters = null;
				stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyUp);
			}
		}
		
		public function get npcId():String
		{
			return _npcId;
		}
		
		public function set npcId(value:String):void
		{
			_npcId = value;
			if(!(this is Teleport))
				_npcIdLabel.text = _npcId;
			else
				_npcIdLabel.visible = false;
		}
		
		/**
		 * 根据当前所在像素级的xy坐标来设置到逻辑位置
		 */		
		public function setLogic():void
		{
			_lp = NumberTool.p2lTransform(this.x, this.y);
			this.x = _lp.x * Tile.TILE_WIDTH + Tile.TILE_WIDTH / 2;
			this.y = _lp.y * Tile.TILE_WIDTH + Tile.TILE_WIDTH / 2;
		}
		
		protected function onMouseDown(evt:MouseEvent):void
		{
			trace(evt.target)
			Common.ui.toolWindow.setSelectedNpc(this);
			this.startDrag();
		}
		
		protected function onMouseUp(evt:MouseEvent):void
		{
			this.stopDrag();
		}
		
		private function onKeyUp(e:KeyboardEvent):void
		{
			if(e.keyCode == Keyboard.UP)
			{
				this.y --;
			}
			else if(e.keyCode == Keyboard.DOWN)
			{
				this.y ++;
			}
			else if(e.keyCode == Keyboard.LEFT)
			{
				this.x --;
			}
			else if(e.keyCode == Keyboard.RIGHT)
			{
				this.x ++;
			}
		}
		//
	}
}